<cfset sdate = Request.rdate>
<cfset wsid = Request.wsid>
<cfset pID = Request.ID>
<cfset stat = ""> 	
<cfset desc="">
<cfset sdesc="">
<cfset hasTempLv = false>

<cfset urldate =CreateODBCDate(sdate)>


<cfquery name="QueryView" datasource="#client.company_dsn#" maxrows="2">
	SELECT ISLEAVE FROM CINWSHISTORY
    WHERE 
    	REFERENCEDATE =  <cfqueryparam cfsqltype="cf_sql_date" value="#urldate#">
    	AND PERSONNELIDNO ='#pID#'
        AND WSDOCNUMBER ='#wsid#'
        AND (APPROVED IS NULL OR APPROVED ='N')
    ORDER BY DATELASTUPDATE DESC, TIMELASTUPDATE DESC
</cfquery>


<cfif QueryView.RecordCount>
	<cfif QueryView.ISLEAVE[1] EQ 'Y'>
    	<cfset hasTempLv = true>
    </cfif>
</cfif>
<cfif not hasTempLv>
    <cfquery name="QueryView" datasource="#client.company_dsn#" maxrows="1">
        SELECT 
                CD.DESCRIPTION,
                SI.APPROVED
            FROM CINLEAVEAPPSD SI
                LEFT JOIN CLKLVCODE CD ON CD.LEAVETYPE = SI.LEAVETYPE
    
        WHERE REFERENCEDATE =  <cfqueryparam cfsqltype="cf_sql_date" value="#urldate#">
        AND SI.PERSONNELIDNO ='#pID#'
    </cfquery>


	<cfif not QueryView.RecordCount>
        <cfquery name="QueryView" datasource="#client.company_dsn#">
            SELECT LK.DESCRIPTION, SI.APPROVED
            FROM CINLEAVEAPPSI SI
    
            LEFT JOIN CLKLVCODE LK ON LK.LEAVETYPE = SI.LEAVETYPE
            WHERE STARTINGDATE >=  <cfqueryparam cfsqltype="cf_sql_date" value="#urldate#"> AND
            	ENDINGDATE <=  <cfqueryparam cfsqltype="cf_sql_date" value="#urldate#">
            AND PERSONNELIDNO = '#pID#'
            AND  SI.APPROVED IS NOT NULL
        </cfquery>
    </cfif>
    <cfif QueryView.RecordCount >
        <cfset stat = QueryView.APPROVED> 	
        <cfset desc = QueryView.DESCRIPTION>
    </cfif>
    
</cfif>
<cfscript>
	if ( not hasTempLv ){
		if(stat neq ''){
			if( stat eq 'Y')
				sdesc = 'APPROVED';
			else if( stat eq 'N')
				sdesc = 'DISAPPROVED';
			else if( stat eq 'B')
				sdesc = 'RETURNED TO IS.';
			else if( stat eq 'R')
				sdesc = 'RETURNED TO ORIG.';
			else if( stat eq 'S') 
				sdesc = 'PENDING FOR APPROVAL';
		}
	}else{
		stat = 'S'; 
		desc = 'Vacation Leave';
		sdesc = 'NOT YET POSTED'; 
	}
</cfscript>

<CFSET caller.STATUS = stat>   
<CFSET caller.LVTYPE = desc>   
<CFSET caller.sDESC = sdesc>


